#!/usr/bin/env python

from FATDE import FATDE
from FATLFN import FATLFN

import FuseStat

class FATFile():
   def __init__(self, entries, fs):
      self.fs = fs
      entries.reverse()
      self.fatde = FATDE(entries[0].buffer)
      
      self.name = self.__getName(entries)
      
   def getName(self):
      return self.name
      
   def getCluster(self):  return self.fatde.cluster()
      
   def isDirectory(self): return self.fatde.isDirectory()

   def cleanShortFileName(self, name):
      base = name[0:8].rstrip(' ')
      ext = name[8:].rstrip(' ')
      if len(ext) > 0:
         base += "." + ext
      return base
      
   #
   #  Calculate the name from the entries, then delete the entries
   #
   def __getName(self, entries):
      # Is there a long file name
      #print len(entries)
      if len(entries) == 1:
         # only one entry
         return self.cleanShortFileName(self.fatde.name)

      # Test, print all the entries
      #for entry in entries:
      #  print "\t", entry
         
      #print

      # Now process the long file name (more than one entry ... should be long file name)
      name = ""
      expected = 1
      done = False
      if not self.fatde.deleted():
         for entry in entries[1:]:
            lfn = FATLFN(entry.buffer)
            if lfn.isLongFileName() and lfn.sequenceNumber() == expected and lfn.chksum == self.fatde.chksum():
               name += lfn.getName()
               if lfn.endSequence():
                  done = True
                  break
               expected += 1
            elif True:
               print "Error processing LFN entries ... "
               print lfn.isLongFileName(), hex(lfn.chksum), hex(self.fatde.chksum()), lfn.sequenceNumber(), expected
               print "\t",self.fatde
               print "\t",lfn
      else:
         # deleted file ... more tenuous
         lfn = FATLFN(entries[1].buffer)
         if lfn.isLongFileName() and lfn.deleted():
            chksum = lfn.chksum
            name = lfn.getName()
            done = True
            for entry in entries[2:]:
               lfn = FATLFN(entries[1].buffer)
               if not lfn.isLongFileName and not lfn.deleted() and lfn.chksum <> chksum:
                  break
               else:
                  name += lfn.getName()

      if not done:
         return self.cleanShortFileName(self.fatde.name)
      else:
         return name
         
   def __str__(self):
      return self.getName()

   # Implemented so we can do the Node thng (like a Java Interface)
   def getChildren(self):
      if self.isDirectory():
         cluster = self.getCluster()
         if cluster in self.fs.directories:
            directory = self.fs.directories[cluster]
            return directory.getChildren()
      return []
      
   def getChild(self, name):
      if self.isDirectory():
         cluster = self.getCluster()
         if cluster in self.fs.directories:
            directory = self.fs.directories[cluster]
            return directory.getChild(name)
      return None

   def getAttribs(self):
      fatde = self.fatde
      return FuseStat.FATStat(fatde)
      
   def getContent(self):
      startCluster = self.fatde.cluster()
      length = self.fatde.size
      return self.fs.read(startCluster, length)

import sys
import Image

if __name__ == '__main__':
   args = sys.argv
   
   # Initialise the image
   image = Image.Image(args[1])

   entries = image.getSectorEntries(15336)
   
   start = 0
   for i in range(len(entries)):
      if not entries[i].isLongFileName():
         # Short file name, process all entries up to this point (should contain all LFN entries)
         f = FATFile(entries[start:i+1])
         print f.getName()
         start = i + 1

   
